//获取应用实例
var app = getApp();

// 初始化数据
var init = {

  // 数据
  data: {
    score1: false,//1打分
    score2: false,//1打分
    score3: false,//1打分
    score4: false,//1打分
    score5: false,//1打分
    score: '',//分数
    title: '',//标题
    image: '',//图片

  },

  // 页面加载
  onLoad: function (options) {
    this.setData({
      options: options
    });
  },

  // 用户输入
  getTextarea: function (event) {
    var txt = event.detail.value
    console.log(txt);
    this.setData({
      textarea: txt
    })
  },

  // 验证
  verify: function (fn) {
    var that = this;
    if (!that.data.textarea) {
      that.tips('请输入评价内容');
      return false;
    } else if (that.data.score == '') {
      that.tips('请给司机评分');
      return false;
    }
    return true;
  },
  // 提交评论信息
  submit: function () {
    var that = this;
    if (that.verify()){
      app.getLoginSession(function (session) {
        console.log(that.data.options);
        wx.request({
          url: app.api + '/wx_log_test/comment/?token='+session,
          data: {
            content: that.data.textarea,
            id: "",
            lastmodifiedOn: "",
            orderId: that.data.options.orderid,
            star: that.data.score,
            userId: that.data.options.driverid
          },
          method: 'POST',
          success: function (res) {
            var res = res.data;

            if (res.msg=="OK") {

              wx.showModal({
                title: '提示',
                content: '评论成功',
                success: function (res) {
                  if (res.confirm) {
                    wx.navigateBack();
                  } else {
                    wx.navigateBack();
                  }

                }
              })
            } else {
              wx.showToast({
                title: '提交失败',
                icon:'loading'
              })
            }
          }, fail: function () {
            wx.showToast({
              title: '提交失败',
              icon: 'loading'
            })
          }
        })
      })
    }
    
  },

  // 提示框
  tips: function (txt) {
    wx.showModal({
      title: '提示',
      content: txt,
      showCancel: false
    })
  }, scoreone: function () {
    var that = this;
    that.setData({
      score1: true,
      score2: false,
      score3: false,
      score4: false,
      score5: false,
      score: 1
    })
  },
  scoretwo: function () {
    var that = this;
    that.setData({
      score1: true,
      score2: true,
      score3: false,
      score4: false,
      score5: false,
      score: 2
    })
  },
  scorethree: function () {
    var that = this;
    that.setData({
      score1: true,
      score2: true,
      score3: true,
      score4: false,
      score5: false,
      score: 3
    })
  },
  scorefour: function () {
    var that = this;
    that.setData({
      score1: true,
      score2: true,
      score3: true,
      score4: true,
      score5: false,
      score: 4
    })
  },
  scorefive: function () {
    var that = this;
    that.setData({
      score1: true,
      score2: true,
      score3: true,
      score4: true,
      score5: true,
      score: 5
    })
  }

};

// 注册页面
Page(init);
